package com.ruoyi.finishedProduct.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.finishedProduct.domain.FinishedProduct;
import com.ruoyi.finishedProduct.domain.FinishedProductModel;
import com.ruoyi.finishedProduct.mapper.FinishedProductMapper;
import com.ruoyi.finishedProduct.service.FinishedProductService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class FinishedProductServiceImpl implements FinishedProductService {
    @Autowired
    FinishedProductMapper finishedProductMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Override
    public int addFinishedProduct(FinishedProduct finishedProduct) {

        int num = 0;

        try {
            if (finishedProduct.getFinishedProductModels() != null) {
                num = finishedProductMapper.addFinishedProduct(finishedProduct);
                for (FinishedProductModel finishedProductModel : finishedProduct.getFinishedProductModels()) {
                    finishedProductModel.setDeptId(SecurityUtils.getDeptId());
                    finishedProductModel.setFinishedProductId(finishedProduct.getFinishedProductId());
                    finishedProductMapper.addFinishedProductModel(finishedProductModel);
                }

            }
        } catch (Exception e) {
            num = 0;
            finishedProductMapper.removeFinishedProduct(finishedProduct.getFinishedProductId());
            finishedProductMapper.removeFinishedProductModel(finishedProduct.getFinishedProductId());
        }
        if (num > 0) {
            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(finishedProduct.getFinishedProductId());
            mtAudit.setZjName(finishedProduct.getFinishedProductModels().get(0).getWarehousingName());
            mtAudit.setOrderType("15");
            mtAudit.setOrderNum(finishedProduct.getFinishedOrderNum());
            mtAudit.setAuditStatus("0");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);
        }

        return num;
    }

    @Override
    public List<FinishedProduct> selectFinishedProductList(FinishedProduct finishedProduct) {
        return finishedProductMapper.selectFinishedProductList(finishedProduct);
    }

    @Override
    public FinishedProduct selectEdit(Long finishedProductId) {
        return finishedProductMapper.selectEdit(finishedProductId);
    }

    @Override
    public void remove(Long finishedProductId) {
        finishedProductMapper.removeFinishedProduct(finishedProductId);
        finishedProductMapper.removeFinishedProductModel(finishedProductId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", finishedProductId);
        resMap.put("orderType", "15");
        mtAuditMapper.deleteMtAuditById(resMap);
    }

    @Override
    public int updateFinishedProduct(FinishedProduct finishedProduct) {
        int num = 0;
        if (finishedProduct.getFinishedProductModels() != null) {
            for (FinishedProductModel finishedProductModel : finishedProduct.getFinishedProductModels()) {
                finishedProductModel.setDeptId(SecurityUtils.getDeptId());
                finishedProductModel.setFinishedProductId(finishedProduct.getFinishedProductId());
                finishedProductMapper.addFinishedProductModel(finishedProductModel);
            }
            num = finishedProductMapper.updateFinishedProduct(finishedProduct);
        }
        return num;
    }
}
